package algotithm.leetcode.string.test1165;

/**
 * @author 25068
 * @Date 2021/4/24 22:46
 * @Title 1165. 单行键盘
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String keyboard = "abcdefghijklmnopqrstuvwxyz";
        String word = "cba";
        int res = solution.calculateTime(keyboard, word);
        System.out.println(res);
    }

    public int calculateTime(String keyboard, String word) {
        int[] map = new int[26];
        int ans = 0;
        for(int i = 0; i < keyboard.length(); i++) {
            map[keyboard.charAt(i) - 'a'] = i;
        }
        int start = 0;
        for(char c : word.toCharArray()) {
            ans += Math.abs((map[c - 'a'] - start));
            start = map[c - 'a'];
        }
        return ans;
    }
}
